//
//  WishItemViewController.m
//  iFoundIt
//
//  Created by Navid Shariat on 4/05/10.
//  Copyright 2010 QUT. All rights reserved.
//

#import "WishItemViewController.h"
#import "StoreListViewController.h"
#import "MyLocationManager.h"
#import "WishlistPersistentManager.h"

@implementation WishItemViewController

@synthesize wishedItem;
@synthesize fromLocationTF;
@synthesize keywordTextField;
@synthesize radiusTextField;
@synthesize saveBtn;
@synthesize viewBtn;
@synthesize statusLabel;
@synthesize wishlistManager;
@synthesize stManager;

WishlistPersistentManager * wishlistManager;

- (void)dealloc {
	if (wishlistManager)
		[wishlistManager release];
	if (stManager)
		[stManager release];
	[wishedItem release];
	[radiusTextField release];
	[keywordTextField release];	
	[fromLocationTF release];
	[statusLabel release];
	[viewBtn release];
	[saveBtn  release];
    [super dealloc];
}

- (void)viewDidLoad {
	[self setTitle:@"Wished Item details"];
	[[self fromLocationTF] setText: [wishedItem fromLocation]];
	[[self keywordTextField] setText: [wishedItem searchKeyword]];
	[[self statusLabel] setText: [wishedItem getItemStatusString]];
	[[self radiusTextField] setText:[NSString stringWithFormat:@"%.2lf",[wishedItem radius]]];
	[[self viewBtn] setHidden: ![wishedItem itemStatus]];
	[[self fromLocationTF] setEnabled:NO];
	[self calcFromLocation:[wishedItem fromLocationCoordinate]];
    [super viewDidLoad];
}

#pragma mark actions
-(IBAction)textFieldDoneEditing:(id)sender{
	[sender resignFirstResponder];
}

- (IBAction) saveButtonPressesd{
	[wishedItem setSearchKeyword:[[self keywordTextField] text]];
	[wishedItem setRadius:[[[self radiusTextField] text] doubleValue]];
	[[self wishlistManager] updateItem:wishedItem];
	 UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Update"
													 message: @"Item updated successfully!"
													delegate: nil
										   cancelButtonTitle: @"OK"
										   otherButtonTitles: nil];
	 
	 [alert show];
	 [alert release];
	 
}

- (IBAction) pickCurrentLocationAction{
	NSString * address= [self getCurrentLocAddress];
	[[self fromLocationTF] setText: address];
}

- (IBAction) backgroundPressed{
	//hides both keyboards
	[[self radiusTextField] resignFirstResponder];
	[[self keywordTextField] resignFirstResponder];
	[[self fromLocationTF] resignFirstResponder];
}

- (IBAction) viewFoundItem{
	CLLocation* location=[[CLLocation alloc] initWithLatitude:[wishedItem fromLocationCoordinate].latitude longitude:[wishedItem fromLocationCoordinate].longitude];
	NSMutableArray* foundStores= [[self stManager] findStores:[wishedItem searchKeyword]: location: [wishedItem radius]*1000];	
	[location release];
	if ([foundStores count]!=0)
		[self navigateToMapView:foundStores:[[self wishedItem] fromLocationCoordinate]];
}

#pragma mark methods
- (void)calcFromLocation: (CLLocationCoordinate2D) coordinate {
	//	MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:[[[self mapView] userLocation] coordinate]];
	MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate: coordinate];
	[reverseGeocoder setDelegate:self];
	[reverseGeocoder start];	
}


-  (void) navigateToMapView: (NSMutableArray *)  stores: (CLLocationCoordinate2D) coordinate{
	StoreMapViewController * storeMapViewController= [[StoreMapViewController alloc] initWithNibName:@"StoreMapViewController" bundle:nil];
	[storeMapViewController setFoundPlaces:stores];
	[storeMapViewController setAlternativeLocationCoordinate:coordinate];
	[[self navigationController] pushViewController:storeMapViewController animated:YES];
	[storeMapViewController release];			
}

- (WishlistPersistentManager*) wishlistManager{
	if (!wishlistManager)
		wishlistManager=[[WishlistPersistentManager alloc] init];
	return wishlistManager;
}

- (StorePersistentManager*) stManager{
	if (!stManager)
		stManager=[[StorePersistentManager alloc] init];
	return stManager;
}

-  (void) navigateToListView: (SearchProps *) searchProps{
	StoreListViewController *storeListViewController= [[StoreListViewController alloc] initWithNibName:@"StoreListViewController" bundle:nil];
	[storeListViewController setSearchProps:searchProps];
	[[self navigationController]pushViewController:storeListViewController animated:YES];
	[storeListViewController release];				
}			

- (NSString *) getCurrentLocAddress{
	return @"QUT Brisbane";
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark rgeocoderDelegate
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
	NSLog(@"didFindPlacemark");
	NSMutableString *locationAddress = [[NSMutableString alloc] init];
	
	if ([[placemark subThoroughfare] length] > 0) {
		[locationAddress appendFormat:@"%@ ", placemark.subThoroughfare];
	}
	
	if ([[placemark thoroughfare] length] > 0) {
		[locationAddress appendFormat:@"%@ ", placemark.thoroughfare];
	}
	
	if ([[placemark subLocality] length] > 0) {
		[locationAddress appendFormat:@"%@ ", placemark.subLocality];
	}
	
	if ([[placemark locality] length] > 0) {
		[locationAddress appendFormat:@"%@ ", placemark.locality];
	}
	[[self fromLocationTF] setText:[NSString stringWithFormat:@"%@",locationAddress]];
	[locationAddress release];	
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
	NSLog(@"reverseGeocoder:didFailWithError");
	
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
													message:@"Sorry, unable to determine the 'form location' field."
												   delegate:nil
										  cancelButtonTitle:@"OK" 
										  otherButtonTitles:nil];
	[alert show];
	[alert release];
}


@end
